#include "MonitoringDeviceTableModel.h"
#include <QDebug>
MonitoringDeviceTableModel::MonitoringDeviceTableModel(QObject *)
{

}

int MonitoringDeviceTableModel::rowCount(const QModelIndex &) const
{
    return deviceList.size();

}

int MonitoringDeviceTableModel::columnCount(const QModelIndex &) const
{
    return 12;
}

QVariant MonitoringDeviceTableModel::data(const QModelIndex &index, int role) const
{
    if (!index.isValid())
        return QVariant();

    if (role == Qt::DisplayRole) {
        const auto &device = deviceList[index.row()];
        switch (index.column()) {
        case 0: return device.id;
        case 1: return device.user_id;
        case 2: return device.name;
        case 3: return device.type;
        case 4: return device.location;
        case 5: return device.manufacturer;
        case 6: return device.model;
        case 7: return device.status;
        case 8: return device.installation_date;
        case 9: return QString("删除");
        case 10: return QString("编辑");
        case 11: return QString("查看");
        default: return QVariant();
        }
    }else if (role == Qt::TextAlignmentRole) {
        return Qt::AlignCenter;
    }
    return QVariant();
}

QVariant MonitoringDeviceTableModel::headerData(int section, Qt::Orientation orientation, int role) const
{
    if (role == Qt::DisplayRole && orientation == Qt::Horizontal) {
        switch (section) {
        case 0: return "设备id";
        case 1: return "用户id";
        case 2: return "设备名称";
        case 3: return "设备类型";
        case 4: return "设备位置";
        case 5: return "设备制造商";
        case 6: return "设备型号";
        case 7: return "设备状态";
        case 8: return "安装日期";
        case 9: return "操作";
        case 10: return "操作";
           case 11: return "操作";
        default: return QVariant();
        }
    }
    return QVariant();
}

void MonitoringDeviceTableModel::setDeviceList(const QList<deviceModel> &list){
    beginResetModel();
    deviceList = list;
    endResetModel();
}
QList<deviceModel> MonitoringDeviceTableModel::getDeviceList() const{
    return deviceList;

}
void MonitoringDeviceTableModel::removeDevice(int row) {

    if (row >= 0 && row < deviceList.size()) {
        deviceModel deviceToDelete = deviceList[row];
        beginRemoveRows(QModelIndex(), row, row);
        deviceList.removeAt(row);
        endRemoveRows();
        emit deviceDelete(deviceToDelete);
    }
}



void MonitoringDeviceTableModel::updateAllDeviceList(const QList<deviceModel> &newList)
{
    beginResetModel();
    deviceList = newList;
    endResetModel();
}

Qt::ItemFlags MonitoringDeviceTableModel::flags(const QModelIndex &index) const
{
    if (!index.isValid())
        return Qt::NoItemFlags;

    if (index.row() == editRow && (index.column() >= 2 && index.column() <= 7)) {
        return QAbstractTableModel::flags(index) | Qt::ItemIsEditable;
    }

    return QAbstractTableModel::flags(index);
}

void MonitoringDeviceTableModel::setEditRow(int row)
{
    qDebug() << "设置编辑行号: " << row;
    if (editRow != -1) {
        QModelIndex oldIndexStart = createIndex(editRow, 2);
        QModelIndex oldIndexEnd = createIndex(editRow, 7);
        emit dataChanged(oldIndexStart, oldIndexEnd);
    }
    editRow = row;
    if (editRow != -1) {
        QModelIndex newIndexStart = createIndex(editRow, 2);
        QModelIndex newIndexEnd = createIndex(editRow, 7);
        emit dataChanged(newIndexStart, newIndexEnd);
    }
}

void MonitoringDeviceTableModel::updateDeviceData(int row, int column, const QVariant &newData)
{
    QModelIndex index = createIndex(row, column);

    auto &device = deviceList[row];
    switch (column) {
    case 0: device.id = newData.toString(); break;
    case 1: device.user_id = newData.toString(); break;
    case 2: device.name = newData.toString(); break;
    case 3: device.type = newData.toString(); break;
    case 4: device.location = newData.toString(); break;
    case 5: device.manufacturer = newData.toString(); break;
    case 6: device.model = newData.toString(); break;
    case 7: device.status = newData.toString(); break;
    case 8: device.installation_date = newData.toString(); break;
    default: break;
    }
    emit dataChanged(index, index);
}

bool MonitoringDeviceTableModel::setData(const QModelIndex &index, const QVariant &value, int role)
{
    if (index.isValid() && role == Qt::EditRole) {
        qDebug() << "尝试更新数据，行号: " << index.row() << " 列号: " << index.column() << " 新值: " << value.toString();
        auto &device = deviceList[index.row()];
        switch (index.column()) {
        case 2: device.name = value.toString(); break;
        case 3: device.type = value.toString(); break;
        case 4: device.location = value.toString(); break;
        case 5: device.manufacturer = value.toString(); break;
        case 6: device.model = value.toString(); break;
        case 7: device.status = value.toString(); break;
        default: return false;
        }
        emit dataChanged(index, index);
        return true;
    }
    return false;
}
